﻿using JumpstartApp.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace JumpstartApp.Models
{
    using JumpstartApp.Helpers;

    public class ParentBase : BindableBase
    {
        [XmlAttribute("Id")]
        public string Id { get; set; }

        [XmlElement("Title")]
        public string Title { get; set; }

        [XmlElement("Link")]
        public string Link { get; set; }

        ObservableCollection<ChildBase> m_Children = new ObservableCollection<ChildBase>();
        public ObservableCollection<ChildBase> Children { get { return m_Children; } private set { m_Children = value; } }

        List<EpisodeRecord> m_Episodes = default(List<EpisodeRecord>);
        [XmlElement("Episode")]
        public List<EpisodeRecord> Episodes
        {
            get { return m_Episodes; }
            set
            {
                SetProperty(ref m_Episodes, value);
                foreach (var item in value)
                    if (!Children.Contains(item))
                        Children.Add(item);
            }
        }

        List<ModuleRecord> m_Modules = default(List<ModuleRecord>);
        [XmlElement("Module")]
        public List<ModuleRecord> Modules
        {
            get { return m_Modules; }
            set
            {
                SetProperty(ref m_Modules, value);
                foreach (var item in value)
                    if (!Children.Contains(item))
                        Children.Add(item);
            }
        }

        public ParentBase Clone()
        {
            var _Children = new ObservableCollection<ChildBase>();
            foreach (var item in Children)
                _Children.Add(item.Clone());
            return new ParentBase
            {
                Children = _Children,
                Id = Id,
                Link = Link,
                Title = Title
            };
        }
    }
}
